<?php
	require_once($_SERVER['DOCUMENT_ROOT']."/classes/Account.class.php");
	require_once($_SERVER['DOCUMENT_ROOT']."/includes/sugar.php");
	session_start();
	
	if (preg_match("([0-9]{4}[-][0-9]{2}[-][0-9]{2})", $_POST['birthDate'], $matches))
	{
		/* Format it to SQL format date */
		$_POST['birthDate'] = preg_replace("/(\d+)-(\d+)-(\d+)/", "$3/$2/$1", $_POST['birthDate']);
	}
	/* Tests if the date is well-formed */
	if(!preg_match("([0-9]{2}[/][0-9]{2}[/][0-9]{4})", $_POST['birthDate'], $matches))
	{
		$array['errMess'] = getErrMess(27);
			$array['link'] = "http://".$_SERVER['SERVER_NAME']."/index.php?module=myaccount";
			$array['status'] = 0;
	}
	else {
		if(strlen($_POST['nickname']) > 0){
			$_SESSION['account']->addMember($_POST);
			if($_SESSION['account']->save()){
				$array['errMess'] = getErrMess(9);
				$array['link'] = "http://".$_SERVER['SERVER_NAME']."/index.php?module=myaccount";
				$array['status'] = 1;
			}
			else
			{
				$array['errMess'] = getErrMess(8);
				$array['link'] = "http://".$_SERVER['SERVER_NAME']."/index.php?module=myaccount";
				$array['status'] = 0;
			}
		}
		else
		{
			$array['errMess'] = getErrMess(37);
			$array['status'] = 0;
		}
	}
	echo json_encode($array);
?>